1
|
|
|
import { |
2
|
|
|
Controller, |
3
|
|
|
Inject, |
4
|
|
|
Body, |
5
|
|
|
BadRequestException, |
6
|
|
|
UseGuards, |
7
|
|
|
Param, |
8
|
|
|
Post |
9
|
|
|
} from '@nestjs/common'; |
10
|
|
|
import { AuthGuard } from '@nestjs/passport'; |
11
|
|
|
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'; |
12
|
|
|
import { ICommandBus } from 'src/Application/ICommandBus'; |
13
|
|
|
import { IQueryBus } from 'src/Application/IQueryBus'; |
14
|
|
|
import { AddUserToSchoolCommand } from 'src/Application/School/Command/User/AddUserToSchoolCommand'; |
15
|
|
|
import { CreateVoucherCommand } from 'src/Application/School/Command/Voucher/CreateVoucherCommand'; |
16
|
|
|
import { GetUserByEmailQuery } from 'src/Application/User/Query/GetUserByEmailQuery'; |
17
|
|
|
import { User, UserRole } from 'src/Domain/User/User.entity'; |
18
|
|
|
import { EmailDTO } from 'src/Infrastructure/Common/DTO/EmailDTO'; |
19
|
|
|
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO'; |
20
|
|
|
import { Roles } from 'src/Infrastructure/User/Decorator/Roles'; |
21
|
|
|
import { RolesGuard } from 'src/Infrastructure/User/Security/RolesGuard'; |
22
|
|
|
|
23
|
|
|
@Controller('schools') |
24
|
|
|
@ApiTags('School user') |
25
|
|
|
@ApiBearerAuth() |
26
|
|
|
@UseGuards(AuthGuard('bearer'), RolesGuard) |
27
|
|
|
export class AddOrInviteUserToSchoolAction { |
28
|
|
|
constructor( |
29
|
|
|
@Inject('ICommandBus') |
30
|
|
|
private readonly commandBus: ICommandBus, |
31
|
|
|
@Inject('IQueryBus') |
32
|
|
|
private readonly queryBus: IQueryBus, |
33
|
|
|
) {} |
34
|
|
|
|
35
|
|
|
@Post(':id/users') |
36
|
|
|
@Roles(UserRole.PHOTOGRAPHER) |
37
|
|
|
@ApiOperation({ summary: 'Add or invite a user to a school' }) |
38
|
|
|
public async index(@Param() { id }: IdDTO, @Body() { email }: EmailDTO) { |
39
|
|
|
try { |
40
|
|
|
const user: User = await this.queryBus.execute(new GetUserByEmailQuery(email)); |
41
|
|
|
if (user instanceof User) { |
42
|
|
|
await this.commandBus.execute(new AddUserToSchoolCommand(user.getId(), id)); |
43
|
|
|
} else { |
44
|
|
|
await this.commandBus.execute(new CreateVoucherCommand(id, email)); |
45
|
|
|
} |
46
|
|
|
} catch (e) { |
47
|
|
|
throw new BadRequestException(e.message); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|